<?php
/**
 * @filename NetQuestionController.php 
 * @encoding UTF-8 
 * @author KangShuAn
 * @datetime 2009-12-3 14:40:12
 * @version 1.0
 * @Description 网点问卷操作
 */
class NetQuestionController extends AdminController{
    private $pagesize = 15;
    /**
     * 网点问卷
     * 一个问卷有哪些网点
     */
    public function actionList(){
        $p = $this->get('p', 1);
        $question_id = intval($this->get('question_id'));
        $netquestion = new NetworkQuestion();
        $criteria = new CDbCriteria();
        $criteria->addCondition('status!='.NetworkQuestion::NETWORK_QUESTION_DELETE);
        $criteria->addColumnCondition(array('question_id' => $question_id));
        $total = $netquestion->count($criteria);
        
        $criteria->limit = $this->pagesize;
        $criteria->offset = ($p - 1) * $this->pagesize;
        $list = $netquestion->findAll($criteria);
        //判断是否有附件
        foreach ($list as $key => $val) {
            $nq_id = $val['nq_id'];
            $list[$key]['haveattach'] = NetworkQuestionAttach::model()->count('nq_id='.$nq_id);
            //echo $val['network_id'].'<br/>';
            $network = NetWork::model()->findByPk($val['network_id'])->attributes;
            //var_dump($network);
            $list[$key]['network_num'] = $network['network_num'];
        }
        $page = new Page($total, $this->pagesize);
        //问卷信息
        $question_info = QuestionBase::model()->findByPk($question_id)->attributes;
		
        $this->smarty->assign('question_info', $question_info);
        $this->smarty->assign('list', $list);
        $this->smarty->assign('netQuestionStatus', NetworkQuestion::networkQuestionStatus());
        $this->smarty->assign('auditStatus', NetworkQuestion::$audit);
        $this->smarty->assign('project_id', $project_id);
        $this->smarty->assign('question_id', $question_id);
        $this->smarty->assign('page', $page->style1());
        $this->smarty->display('netquestion/list.tpl');
    }
    /**
     * 删除网点问卷
     */
    public function actionDel(){
        $nq_id = (int)$this->post('nq_id');
        $netquestion = new NetworkQuestion();
        $res = $netquestion->updateByPk($nq_id, array('status' => 0));
        if($res){
            $this->_ajax(array('status' => 1,'note' => '删除成功'));
        }else{
            $this->_ajax(array('status' => 0,'note' => '删除失败'));
        }
    }
    /**
     * 编辑网点问卷的回答
     */
    public function actionEdit(){
        $nq_id = (int)$this->get('nq_id');
        //网点问卷信息
        $netquestion = new NetworkQuestion();
        $netquestion_info = $netquestion->findByPk($nq_id)->attributes;
        //网点信息
        $network = new NetWork();
        $network_info = $network->findByPk($netquestion_info['network_id'])->attributes;
        $this->smarty->assign('network_info', $network_info);
        //上级网点信息
        if ($network_info) {
            $up_network_info = $network->findByPk($network_info['up_network_id']);
            $this->smarty->assign('up_network_info', $up_network_info);
        }
        
        //问卷基本信息
        $question_id = $netquestion_info['question_id'];
        $question = new QuestionBase();
        $question_info = $question->findByPk($question_id)->attributes;
        
        //问卷题目
        $questionsubject = new QuestionSubject();
        $subject_list = $questionsubject->getByGroupClassify($question_id);

        //问卷基本扩展基本信息，从题目里面出
        $basic_list = $questionsubject->getBasic($question_id);
        
        //问卷的区块
        $subjectclassify = new QuestionSubjectClassify();
        $classify_list = $subjectclassify->getByQuestion($question_id);
        
        //问题的选项
        $questionoption = new QuestionOption();
        $option = $questionoption->getByQuestion($question_id);
        //问卷回答
        $answer = new NetworkQuestionAnswer();
        $answer_list = $answer->getByNq($nq_id);
        
        //回答的附件
        $attach = new NetworkQuestionAttach();
        $attach_list = $attach->getByNq($nq_id);
        
        $this->smarty->assign('benchmark_list', $benchmark_list);
        $this->smarty->assign('netquestion_info', $netquestion_info);
        $this->smarty->assign('question_info', $question_info);
        $this->smarty->assign('subject_list', $subject_list);
        $this->smarty->assign('basic_list', $basic_list);
        $this->smarty->assign('classify_list', $classify_list);
        $this->smarty->assign('option_list', $option);
        $this->smarty->assign('answer_list', $answer_list);
        $this->smarty->assign('attach_list', $attach_list);
        $this->smarty->assign('nq_id', $nq_id);
        $this->smarty->display('netquestion/edit.tpl');       
    }
    /**
     * 网点问卷预览---回答操作
     */
    public function actionPreview(){
        $nq_id = (int)$this->get('nq_id');
        //网点问卷信息
        $netquestion = new NetworkQuestion();
        $netquestion_info = $netquestion->findByPk($nq_id)->attributes;
        //网点信息
        $network = new NetWork();
        $network_info = $network->findByPk($netquestion_info['network_id'])->attributes;
        $this->smarty->assign('network_info', $network_info);
        //上级网点信息
        if ($network_info) {
            $up_network_info = $network->findByPk($network_info['up_network_id']);
            $this->smarty->assign('up_network_info', $up_network_info);
        }
        
        //问卷基本信息
        $question_id = $netquestion_info['question_id'];
        $question = new QuestionBase();
        $question_info = $question->findByPk($question_id)->attributes;
        
        //问卷题目
        $questionsubject = new QuestionSubject();
        $subject_list = $questionsubject->getByGroupClassify($question_id);

        //问卷基本扩展基本信息，从题目里面出
        $basic_list = $questionsubject->getBasic($question_id);
        
        //问卷的区块
        $subjectclassify = new QuestionSubjectClassify();
        $classify_list = $subjectclassify->getByQuestion($question_id);
		
        //问题的选项
        $questionoption = new QuestionOption();
        $option = $questionoption->getByQuestion($question_id);
        //问卷回答
        $answer = new NetworkQuestionAnswer();
        $answer_list = $answer->getByNq($nq_id);
        
        //回答的附件
        $attach = new NetworkQuestionAttach();
        $attach_list = $attach->getByNq($nq_id);

        //题目是否有异议
        $benchmark = new QuestionBenchmark();
        $benchmark_list = $benchmark->getByNq($nq_id);
        $this->smarty->assign('basic_list', $basic_list);
        $this->smarty->assign('benchmark_list', $benchmark_list);
        $this->smarty->assign('netquestion_info', $netquestion_info);
        $this->smarty->assign('question_info', $question_info);
        $this->smarty->assign('subject_list', $subject_list);
        $this->smarty->assign('classify_list', $classify_list);
        $this->smarty->assign('option_list', $option);
        $this->smarty->assign('answer_list', $answer_list);
        $this->smarty->assign('attach_list', $attach_list);
        $this->smarty->assign('nq_id', $nq_id);
        $this->smarty->display('netquestion/preview.tpl');
    }
    /**
     * 问题的附件显示，即回答的证据附件
     */
    public function actionAttach()
    {
        $nq_id = (int)$this->get('nq_id');
        $subject_id = (int)$this->get('subject_id');
        //网点信息
        $netquestion = new NetworkQuestion();
        $net_info = $netquestion->findByPk($nq_id)->attributes;
        $this->smarty->assign('net_info', $net_info);
        //问卷名称
        $question = new QuestionBase();
        $question_info = $question->findByPk($net_info['question_id'])->attributes;
        $this->smarty->assign('question_info', $question_info);
        //题目信息
        $subject = new QuestionSubject();
        $subject_info = $subject->findByPk($subject_id);
        $this->smarty->assign('subject_info', $subject_info);
        $attach = new NetworkQuestionAttach();
        $list = $attach->getBySubject($nq_id, $subject_id);
        //print_r($list);
        //对标信息
        $answer = NetworkQuestionAnswer::model()->getBySubject($nq_id, $subject_id);
        $this->smarty->assign('answer', $answer);
        $this->smarty->assign('list', $list);
        $this->smarty->display('netquestion/attach.tpl');
    }
    /**
     * 删除附件
     * 对标附件
     */
    public function actionDelattach()
    {
        $nqa_id = (int)$this->get('nqa_id');
        $attach = new NetworkQuestionAttach();
        if ($attach->deleteByPk($nqa_id)) {
            $this->_ajax(array('status' => 1,'note' => '操作成功'));
        } else {
            $this->_ajax(array('status' => 0,'note' => '操作失败'));
        }
    }
    /**
     * 删除问题所有附件
     * 对标附件
     */
    public function actionDelallattach()
    {
        $nq_id = (int)$this->get('nq_id');
        $subject_id = (int)$this->get('subject_id');
        $attach = new NetworkQuestionAttach();
        if ($attach->deleteAll("nq_id=".$nq_id." and subject_id=".$subject_id)) {
            $this->_ajax(array('status' => 1,'note' => '操作成功'));
        } else {
            $this->_ajax(array('status' => 0,'note' => '操作失败'));
        }
    }
    /**
     * 回答问题
     */
    public function actionAnswer()
    {
        $nq_id = (int)$this->get('nq_id');
        $subject_id = (int)$this->get('subject_id');
        //题目内容
        $subject = new QuestionSubject();
        $subject_info = $subject->findByPk($subject_id);
        //是否选择题,选项
        if ($subject_info['question_type'] == 2) {
            $question_id = $subject_info['question_id'];
            $option = new QuestionOption();
            $option_info = $option->find('question_id='.$question_id.' and subject_id='.$subject_id);
            $select = json_decode($option_info['content'], true);
            $this->smarty->assign('select', $select);
        }
        //此题目已有的回答
        $answer = new NetworkQuestionAnswer();
        $answer_info = $answer->getBySubject($nq_id, $subject_id, false);
        //此问题的附件
        $attach = new NetworkQuestionAttach();
        $attach_list = $attach->getBySubject($nq_id, $subject_id);
        $this->smarty->assign('attach_list', $attach_list);
        $this->smarty->assign('answer_info', $answer_info);
        $this->smarty->assign('nq_id', $nq_id);
        $this->smarty->assign('subject_id', $subject_id);
        $this->smarty->assign('subject_info', $subject_info);
        $this->smarty->display('netquestion/answer.tpl');
    }
    /**
     * 回答
     */
    public function actionDoAnswer()
    {
        $answer_id = $this->post('answer_id');
        $nq_id = intval($this->post('nq_id'));
        $subject_id = intval($this->post('subject_id'));
        $answer_score = $this->post('answer_score');
        $answer_option = $this->post('answer_option');
        $answer_desc = $this->post('answer_desc');
        
        if ($answer_id) {
            $answer = NetworkQuestionAnswer::model()->findByPk($answer_id);
            //判断分值是否变化，变化后操作总分变化
            $score = $answer->answer_score;
            if ($answer_score != $score) 
            {
                $chae = intval($answer_score - $score);
                $netquestion = NetworkQuestion::model()->findByPk($answer->nq_id);
                $netquestion->score += $chae;
                $netquestion->save();
            }
        } else {
            $answer = new NetworkQuestionAnswer();
        }
        if ($answer_option) 
        {
            $option = join(',',$answer_option);
            $answer->answer_option = $option;
        }
        $answer->nq_id = $nq_id;
        $answer->subject_id = $subject_id;
        $answer->answer_desc = $answer_desc;
        $answer->create_time = time();
        $answer->answer_score = $answer_score;
        if ($answer->save()) {
            $this->_ajax(array('status' => 1,'note' => '操作成功'));
        } else {
            $this->_ajax(array('status' => 0,'note' => '操作失败'));
        }
    }
    /**
     * 修改基本信息
     */
    public function actionBasic()
    {
        $nq_id = (int)$this->get('nq_id');
        $subject_id = (int)$this->get('subject_id');
        //题目内容
        $subject = new QuestionSubject();
        $subject_info = $subject->findByPk($subject_id);

        //此题目已有的回答
        $answer = new NetworkQuestionAnswer();
        $answer_info = $answer->getBySubject($nq_id, $subject_id, false);

        $this->smarty->assign('answer_info', $answer_info);
        $this->smarty->assign('nq_id', $nq_id);
        $this->smarty->assign('subject_id', $subject_id);
        $this->smarty->assign('subject_info', $subject_info);
        $this->smarty->display('netquestion/basic.tpl');
    }
    /**
     * 修改基本信息
     */
    public function actionDobasic()
    {
        $answer_id = $this->post('answer_id');
        $nq_id = intval($this->post('nq_id'));
        $subject_id = intval($this->post('subject_id'));
        $answer_desc = $this->post('answer_desc');
        
        if ($answer_id) {
            $answer = NetworkQuestionAnswer::model()->findByPk($answer_id);
        } else {
            $answer = new NetworkQuestionAnswer();
        }
        $answer->nq_id = $nq_id;
        $answer->subject_id = $subject_id;
        $answer->answer_desc = $answer_desc;
        $answer->create_time = time();
        if ($answer->save()) {
            $this->_ajax(array('status' => 1,'note' => '操作成功'));
        } else {
            $this->_ajax(array('status' => 0,'note' => '操作失败'));
        }
    }
    /**
     * 后台管理员操作历史
     */
    public function actionHistory()
    {
        $nq_id = (int)$this->get('nq_id');
        $subject_id = (int)$this->get('subject_id');
        //前台网点的审核操作
        $benchmark = new QuestionBenchmark();
        $list = $benchmark->getBySubject($nq_id, $subject_id);
        //后台网点对前台审核的回复
        $benchmarkmanage = new QuestionBenchmarkManage();
        $managelist = $benchmarkmanage->getBySubject($nq_id, $subject_id);
        $this->smarty->assign('managelist', $managelist);
        $this->smarty->assign('list', $list);
        $this->smarty->assign('level', NetWork::networkLevel());
        $this->smarty->assign('nq_id', $nq_id);
        $this->smarty->assign('subject_id', $subject_id);
        $this->smarty->display('netquestion/history.tpl');
    }
    /**
     * 前台网点对标操作历史附件
     * 
     */
    public function actionHistoryattach()
    {
        $nq_id = (int)$this->get('nq_id');
        $subject_id = (int)$this->get('subject_id');
        //网点信息
        $netquestion = new NetworkQuestion();
        $net_info = $netquestion->findByPk($nq_id)->attributes;
        $this->smarty->assign('net_info', $net_info);
        //问卷名称
        $question = new QuestionBase();
        $question_info = $question->findByPk($net_info['question_id'])->attributes;
        $this->smarty->assign('question_info', $question_info);
        //题目信息
        $subject = new QuestionSubject();
        $subject_info = $subject->findByPk($subject_id);
        $this->smarty->assign('subject_info', $subject_info);
        
        $attach = new QuestionBenchmarkAttach();
        $list = $attach->getBySubject($nq_id, $subject_id, $net_info['network_id']);
        $this->smarty->assign('list', $list);
        $this->smarty->display('netquestion/history_attach.tpl');
    }
    /**
     * 后台管理员还分操作
     */
    public function actionHuanfen()
    {
       $nq_id = $this->get('nq_id');
       $subject_id = $this->get('subject_id');
       $qbm_id = intval($this->get('qbm_id'));
       if ($qbm_id) 
       {
           $qbm = QuestionBenchmarkManage::model()->findByPk($qbm_id)->attributes;
           $this->smarty->assign('qbm', $qbm);
           $nq_id = $qbm['nq_id'];
           $subject_id = $qbm['subject_id'];
       }
       //查找这个题目的标准分
       $question = QuestionSubject::model()->findByPk($subject_id)->attributes;

       $this->smarty->assign('nq_id', $nq_id);
       $this->smarty->assign('subject_id', $subject_id);
       $this->smarty->assign('question', $question);
       $this->smarty->display('netquestion/huanfen.tpl');
    }
    /**
     * 后台管理员对前台对标申请的操作
     * 主要是还分
     */
    public function actionDohuanfen()
    { 
        $qbm_id = (int)$this->post('qbm_id');
        $nq_id = (int)$this->post('nq_id');
        $subject_id = (int)$this->post('subject_id');
        $isreturn = $this->post('isreturn');
        if ($qbm_id) {
            $qbenchmark = QuestionBenchmarkManage::model()->findByPk($qbm_id);
        } else {
            $qbenchmark = new QuestionBenchmarkManage();
        }
        
        //查找此题目的标准分
        $subject_info = QuestionSubject::model()->findByPk($subject_id);
        $standard_score = $subject_info['standard_score'];
        if ($isreturn > $standard_score) {
            $this->_ajax(array('status' => 0,'note' => '还分超过标准分'.$standard_score.'分'));
        }
        
        $qbenchmark->suggestion = $this->post('suggestion');
        $qbenchmark->isreturn = $isreturn;
        $qbenchmark->update_time = date('Y-m-d H:i:s');
        $qbenchmark->user_id = $this->user_id;
        $qbenchmark->nq_id = $nq_id;
        $qbenchmark->subject_id = $subject_id;

        //查找后台用户信息
        $user = ManageUser::model()->findByPk($this->user_id)->attributes;
        $qbenchmark->truename = $user['truename'];
        
        if ($qbenchmark->save()){
            //修改对应的申请还分的操作为已还分状态
            if ($isreturn > 0) 
            {
                //如果还分的话是否需要将分数修改，只能还一次分数
                //修改题目分数
                $answer = NetworkQuestionAnswer::model()->findByAttributes(array('nq_id' => $nq_id,'subject_id' => $subject_id));
                $score = intval($isreturn) - intval($answer->answer_score);
                $answer->answer_score =  $isreturn;
                $answer->save();
                //修改总分数
                $netquestion = NetworkQuestion::model()->findByPk($nq_id);
                $netquestion->score += $score;
                $netquestion->save();
            } 
            $this->_ajax(array('status' => 1,'note' => '成功'));
        } else {
            $this->_ajax(array('status' => 0,'note' => '失败'));
        }
    }
    /**
     * 后台批量修改问卷状态
     */
    public function actionSetStatus(){
        $nq_id = $this->post('nq_id');
        $networkquestion = new NetworkQuestion();
        $info = $networkquestion->findByPk($nq_id[0])->attributes;
        $question_id = intval($info['question_id']);

        if ($networkquestion->updateByPk($nq_id, array('status' => NetworkQuestion::NETWORK_QUESTION_COMPLETE))) {
            $total = $networkquestion->count('question_id='.$question_id.' and status='.NetworkQuestion::NETWORK_QUESTION_COMPLETE);
            $question = new QuestionBase();
            $question->updateByPk($question_id, array('complete_question_num' => $total));
            $this->_ajax(array('status' => 1, 'note' => '设置成功'));
        } else {
            $this->_ajax(array('status' => 0, 'note' => '设置失败'));
        }
    }
}
